package com.ffs.game.scene.obj.ai.fsm;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.ffs.game.animation.AnimationManager;
import com.ffs.game.scene.GameScene;
import com.ffs.game.scene.obj.Monster;
import com.ffs.game.scene.obj.ai.MonsterState;

/**
 * 状态机状态抽象
 *
 * @author ffs80
 */
public abstract class AbstractFSMState<S extends MonsterState, T extends Monster> {

    /**
     * 状态
     */
    protected S state;

    /**
     * 怪物对象
     */
    protected T monster;

    /**
     * 状态动画
     */
    protected Animation animation;

    public AbstractFSMState(S state, T monster) {
        this.state = state;
        this.monster = monster;
        initAnimation();
    }

    /**
     * 初始化动画
     */
    public void initAnimation(){
        animation = AnimationManager.getInstance().findAnimation(state);
    }

    /**
     * 进入状态
     */
    public abstract void enter(GameScene screen);

    /**
     * 退出状态
     */
    public abstract void exit(GameScene screen);

    /**
     * 状态更新
     */
    public abstract void update(GameScene screen, int delta);

    /**
     * 状态切换检测
     */
    public abstract S checkTransfer(GameScene screen);

    public S findState() {
        return state;
    }

    /**
     * 获取动画当前帧
     */
    protected TextureRegion findCurFrame() {
        float deltaTime = monster.getDeltaTime();
        if (animation == null) {
            return null;
        }

        return animation.getKeyFrame(deltaTime,true);
    }
}
